<?php
/**
 * 同城微聚汇
 * ============================================================================
 * * 版权所有 2015-2027 武汉鼎联信息技术有限公司，并保留所有权利。
 * 网站地址: http://dlshop.dliansoft.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 当燃   2016-05-10
 */ 
namespace app\mobile\controller;
use think\Db;
use think\Page;

class Activity extends MobileBase {
    public function index(){      
        return $this->fetch();
    }
   /**
    * 商品详情页
    */ 
    public function group(){
         //form表单提交
         C('TOKEN_ON',true);  
         $goodsLogic = new \app\home\logic\GoodsLogic();
         $goods_id = I("get.id/d",66);

         $geturl = I('dlsign');
         $getLink = geturl($geturl);
         $u_id_two = I('u_id');
         $u_id = $getLink['u_id'];
         $goodsId = I('id');
         $n_user = get_user_id();
         $now_user = $n_user['user_id'];
         $share_mdl = M('share_record');

         if($now_user != $u_id_two){
             $where['s_user_id'] = $now_user;
             $where['goods_id'] = $goodsId;
             $share = $share_mdl->where($where)->count();
             $where_one['user_id'] = $now_user;
             $where_one['goods_id'] = $goodsId;
             $share_one = $share_mdl->where($where_one)->count();
             if($share <= 0 && $share_one <= 0){
                 $sWhere['s_user_id'] = $u_id;
                 $sWhere['goods_id'] = $goodsId;
                 $reShareCount = $share_mdl->where($sWhere)->count();
                 $reShare = $share_mdl->where($sWhere)->find();
                 if ($reShareCount <= 0) {
                     $add['user_id'] = $u_id;
                     $add['s_user_id'] = $now_user;
                     $add['goods_id'] = $goodsId;
                     $add['create_time'] = time();
                     $add['link'] = $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
                     $share_mdl->add($add);
                 } else {
                     $add['user_id'] = $u_id;
                     $add['s_user_id'] = $now_user;
                     $add['goods_id'] = $goodsId;
                     $add['create_time'] = time();
                     $add['s_id'] = $reShare['id'];
                     $add['first_user_id'] = $reShare['user_id'];
                     $add['link'] = $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
                     $share_mdl->add($add);
                 }
             }
         }
         $group_buy_info = M('GroupBuy')->where(['goods_id'=>$goods_id,'start_time'=>['<=',time()],'end_time'=>['>=',time()]])->find(); // 找出这个商品
         if(empty($group_buy_info)) 
         {
             //$this->error("此商品没有团购活动",U('Home/Goods/goodsInfo',array('id'=>$goods_id)));
         }
                     
         $goods = M('Goods')->where("goods_id", $goods_id)->find();
         $goods_images_list = M('GoodsImages')->where("goods_id", $goods_id)->select(); // 商品 图册
                 
         $goods_attribute = M('GoodsAttribute')->getField('attr_id,attr_name'); // 查询属性
         $goods_attr_list = M('GoodsAttr')->where("goods_id", $goods_id)->select(); // 查询商品属性表
                         
         // 商品规格 价钱 库存表 找出 所有 规格项id
         $keys = M('SpecGoodsPrice')->where("goods_id", $goods_id)->getField("GROUP_CONCAT(`key` SEPARATOR '_') ");
         if($keys)
         {
              $specImage =  M('SpecImage')->where(['goods_id'=>$goods_id,'src'=>['<>','']])->getField("spec_image_id,src");// 规格对应的 图片表， 例如颜色
             // var_dump($keys);  
              $keys = str_replace("_","','",$keys);  
              //var_dump($keys);  
              $sql  = "SELECT a.name,a.order,b.* FROM __PREFIX__spec AS a INNER JOIN __PREFIX__spec_item AS b ON a.id = b.spec_id WHERE b.id IN('".$keys."') ORDER BY a.order,b.spec_id";
              $filter_spec2 = DB::query($sql);
             // var_dump(DB::getLastSql());
              //var_dump($filter_spec2 );
              foreach($filter_spec2 as $key => $val)
              {                                  
                  $filter_spec[$val['name']][] = array(
                      'item_id'=> $val['id'],
                      'item'=> $val['item'],
                      'src'=>$specImage[$val['id']],
                      );                 
              }            
         }  

         //var_dump($filter_spec );

         $spec_goods_price  = M('spec_goods_price')->where("goods_id", $goods_id)->getField("key,price,store_count"); // 规格 对应 价格 库存表
         M('Goods')->where("goods_id", $goods_id)->save(array('click_count'=>$goods['click_count']+1 )); // 统计点击数
         $commentStatistics = $goodsLogic->commentStatistics($goods_id);// 获取某个商品的评论统计
         $goods_collect_count = M('goods_collect')->where(array("goods_id"=>$goods_id))->count(); //商品收藏数

        //url加密
        $link = array(
            'u_id' => $n_user['user_id'],
        );
        $shareLink = encrypt_param($link);
        $shareLink = "http://$_SERVER[HTTP_HOST]/index.php?m=Mobile&c=Activity&a=group&id=$goods[goods_id]&dltoken=".$_SESSION['dltoken']."&dlsign=".$shareLink;
        $this->assign('shareLink', $shareLink);

         $this->assign('user',$n_user['user_id']);

         $this->assign('group_buy_info',$group_buy_info);
         $this->assign('spec_goods_price', json_encode($spec_goods_price,true)); // 规格 对应 价格 库存表
         $this->assign('commentStatistics',$commentStatistics);
         $this->assign('goods_attribute',$goods_attribute);
         $this->assign('goods_attr_list',$goods_attr_list);
         $this->assign('filter_spec',$filter_spec);
         $this->assign('goods_images_list',$goods_images_list);
         $this->assign('goods',$goods);
         $this->assign('goods_collect_count',$goods_collect_count); //商品收藏人数
         $this->assign('time',time());
         $this->assign('level',$n_user['level']);
         return $this->fetch();
    }
    
    
    
    
    /**
     * 团购活动列表
     */
   /* public function group_list()
    {
        $istype =I('get.type');
        //以最新新品排序
        if($istype == 'new'){
            $orderby = 'start_time desc';
        }
    	$count =  M('GroupBuy')->where(time()." >= start_time and ".time()." <= end_time ")->count();// 查询满足要求的总记录数
        $pagesize = C('PAGESIZE');  //每页显示数
    	$Page = new Page($count,$pagesize); // 实例化分页类 传入总记录数和每页显示的记录数
    	$show = $Page->show();  // 分页显示输出
    	$this->assign('page',$show);    // 赋值分页输出
        $list = M('GroupBuy')->where(time()." >= start_time and ".time()." <= end_time ")->order($orderby)->limit($Page->firstRow.','.$Page->listRows)->select();   // 找出这个商品
        $this->assign('list', $list);
        if(I('is_ajax')) {
            return $this->fetch('ajax_group_list');      //输出分页
        }
        return $this->fetch();
    }*/
     /**
     * 商品搜索列表页
     */
    public function group_list(){
        $filter_param = array(); // 帅选数组
        $id = I('get.id/d',0); // 当前分类id
        $brand_id = I('brand_id/d',0);              
        $sort = I('sort','goods_id'); // 排序
        $sort_asc = I('sort_asc','desc'); // 排序
        $price = I('price',''); // 价钱
        $start_price = trim(I('start_price','0')); // 输入框价钱
        $end_price = trim(I('end_price','0')); // 输入框价钱
        if($start_price && $end_price) $price = $start_price.'-'.$end_price; // 如果输入框有价钱 则使用输入框的价钱       
        $filter_param['id'] = $id; //加入帅选条件中
        $brand_id  && ($filter_param['brand_id'] = $brand_id); //加入帅选条件中                
        $price  && ($filter_param['price'] = $price); //加入帅选条件中
        $q = urldecode(trim(I('q',''))); // 关键字搜索
        $q  && ($_GET['q'] = $filter_param['q'] = $q); //加入帅选条件中
        $qtype = I('qtype','');
        $where  = array('is_on_sale' => 1);
        if($qtype){
            $filter_param['qtype'] = $qtype;
            $where[$qtype] = 1;
        }
        if($q) $where['goods_name'] = array('like','%'.$q.'%');
        
        $goodsLogic = new \app\home\logic\GoodsLogic(); // 前台商品操作逻辑类
        $filter_goods_id = M('goods')->where($where)->cache(true)->getField("goods_id",true);

        // 过滤帅选的结果集里面找商品
        if($brand_id || $price)// 品牌或者价格
        {
            $goods_id_1 = $goodsLogic->getGoodsIdByBrandPrice($brand_id,$price); // 根据 品牌 或者 价格范围 查找所有商品id
            $filter_goods_id = array_intersect($filter_goods_id,$goods_id_1); // 获取多个帅选条件的结果 的交集
        }

        //筛选网站自营,入驻商家,货到付款,仅看有货,促销商品
        $sel = I('sel');
        if($sel)
        {
            $goods_id_4 = $goodsLogic->get_filter_selected($sel);
            $filter_goods_id = array_intersect($filter_goods_id,$goods_id_4);
        }

        $filter_menu  = $goodsLogic->get_filter_menu($filter_param,'search'); // 获取显示的帅选菜单
        $filter_price = $goodsLogic->get_filter_price($filter_goods_id,$filter_param,'search'); // 帅选的价格期间
        $filter_brand = $goodsLogic->get_filter_brand($filter_goods_id,$filter_param,'search',1); // 获取指定分类下的帅选品牌        
        
        $count = count($filter_goods_id);
        $page = new Page($count,6);
        if($count > 0)
        {
            //$goods_list = M('group_buy')->table('tp_group_buy a,tp_goods b')->where('a.goods_id=b.goods_id')->where("a.goods_id", "in", implode(',', $filter_goods_id))->order("$sort $sort_asc")->limit($page->firstRow.','.$page->listRows)->select();
            $goods_list =M('group_buy a')->join('tp_goods b',' a.goods_id=b.goods_id','left')->field('a.*,b.sales_sum,b.shop_price')->where("a.goods_id","in", implode(',', $filter_goods_id))->order("b.$sort $sort_asc,b.goods_id desc")->limit($page->firstRow.','.$page->listRows)->select();
            $filter_goods_id2 = get_arr_column($goods_list, 'goods_id');
            if($filter_goods_id2)
                $goods_images = M('goods_images')->where("goods_id", "in", implode(',', $filter_goods_id2))->cache(true)->select();
        }
        $goods_category = M('goods_category')->where('is_show=1')->cache(true)->getField('id,name,parent_id,level'); // 键值分类数组
        $u_id = get_user_id();
        $this->assign('user',$u_id['user_id']);
        $this->assign('goods_list',$goods_list);
        $this->assign('goods_category',$goods_category);
        $this->assign('goods_images',$goods_images);  // 相册图片
        $this->assign('filter_menu',$filter_menu);  // 帅选菜单     
        $this->assign('filter_brand',$filter_brand);// 列表页帅选属性 - 商品品牌
        $this->assign('filter_price',$filter_price);// 帅选的价格期间      
        $this->assign('filter_param',$filter_param); // 帅选条件        
        $this->assign('page',$page);// 赋值分页输出
        $this->assign('time',time());// 赋值分页输出
        $this->assign('sort_asc', $sort_asc == 'asc' ? 'desc' : 'asc');
        
        C('TOKEN_ON',false);
        if(input('is_ajax'))
            return $this->fetch('ajaxGroupsList');
        else
            return $this->fetch();
    }

     /**
     * 秒杀产品页面
     */
    public function flash_list(){
        $filter_param = array(); // 帅选数组
        $id = I('get.id/d',0); // 当前分类id
        $brand_id = I('brand_id/d',0);              
        $sort = I('sort','goods_id'); // 排序
        $sort_asc = I('sort_asc','desc'); // 排序
        $price = I('price',''); // 价钱
        $start_price = trim(I('start_price','0')); // 输入框价钱
        $end_price = trim(I('end_price','0')); // 输入框价钱
        if($start_price && $end_price) $price = $start_price.'-'.$end_price; // 如果输入框有价钱 则使用输入框的价钱       
        $filter_param['id'] = $id; //加入帅选条件中
        $brand_id  && ($filter_param['brand_id'] = $brand_id); //加入帅选条件中                
        $price  && ($filter_param['price'] = $price); //加入帅选条件中
        $q = urldecode(trim(I('q',''))); // 关键字搜索
        $q  && ($_GET['q'] = $filter_param['q'] = $q); //加入帅选条件中
        $qtype = I('qtype','');
        $where  = array('is_on_sale' => 1);
        if($qtype){
            $filter_param['qtype'] = $qtype;
            $where[$qtype] = 1;
        }
        if($q) $where['goods_name'] = array('like','%'.$q.'%');
        
        $goodsLogic = new \app\home\logic\GoodsLogic(); // 前台商品操作逻辑类
        $filter_goods_id = M('goods')->where($where)->cache(true)->getField("goods_id",true);

        // 过滤帅选的结果集里面找商品
        if($brand_id || $price)// 品牌或者价格
        {
            $goods_id_1 = $goodsLogic->getGoodsIdByBrandPrice($brand_id,$price); // 根据 品牌 或者 价格范围 查找所有商品id
            $filter_goods_id = array_intersect($filter_goods_id,$goods_id_1); // 获取多个帅选条件的结果 的交集
        }

        //筛选网站自营,入驻商家,货到付款,仅看有货,促销商品
        $sel = I('sel');
        if($sel)
        {
            $goods_id_4 = $goodsLogic->get_filter_selected($sel);
            $filter_goods_id = array_intersect($filter_goods_id,$goods_id_4);
        }

        $filter_menu  = $goodsLogic->get_filter_menu($filter_param,'search'); // 获取显示的帅选菜单
        $filter_price = $goodsLogic->get_filter_price($filter_goods_id,$filter_param,'search'); // 帅选的价格期间
        $filter_brand = $goodsLogic->get_filter_brand($filter_goods_id,$filter_param,'search',1); // 获取指定分类下的帅选品牌        
        
        $count = count($filter_goods_id);
        $page = new Page($count,6);
        if($count > 0)
        {
            //$goods_list = M('group_buy')->table('tp_group_buy a,tp_goods b')->where('a.goods_id=b.goods_id')->where("a.goods_id", "in", implode(',', $filter_goods_id))->order("$sort $sort_asc")->limit($page->firstRow.','.$page->listRows)->select();
            $goods_list =M('flash_sale a')->join('tp_goods b',' a.goods_id=b.goods_id','left')->field('a.*,b.sales_sum,b.shop_price,b.market_price')->where("a.goods_id","in", implode(',', $filter_goods_id))->order("b.$sort $sort_asc,b.goods_id desc")->limit($page->firstRow.','.$page->listRows)->select();
            $filter_goods_id2 = get_arr_column($goods_list, 'goods_id');
            if($filter_goods_id2)
                $goods_images = M('goods_images')->where("goods_id", "in", implode(',', $filter_goods_id2))->cache(true)->select();
        }
        $goods_category = M('goods_category')->where('is_show=1')->cache(true)->getField('id,name,parent_id,level'); // 键值分类数组
        $u_id = get_user_id();
        $this->assign('user',$u_id['user_id']);
        $this->assign('goods_list',$goods_list);
        $this->assign('goods_category',$goods_category);
        $this->assign('goods_images',$goods_images);  // 相册图片
        $this->assign('filter_menu',$filter_menu);  // 帅选菜单     
        $this->assign('filter_brand',$filter_brand);// 列表页帅选属性 - 商品品牌
        $this->assign('filter_price',$filter_price);// 帅选的价格期间      
        $this->assign('filter_param',$filter_param); // 帅选条件        
        $this->assign('page',$page);// 赋值分页输出
        $this->assign('time',time());// 赋值分页输出
        $this->assign('sort_asc', $sort_asc == 'asc' ? 'desc' : 'asc');
        
        C('TOKEN_ON',false);
        if(input('is_ajax'))
            return $this->fetch('ajaxGroupsList');
        else
            return $this->fetch();
    }

    /**
     * 活动商品列表
     */
    public function discount_list(){
        $prom_id =I('id/d');    //活动ID
        $where = array(     //条件
            'prom_type'=>3,
            'prom_id'=>$prom_id,
        );
        $pagesize = C('PAGESIZE');  //每页显示数
    	$count =  M('goods')->where($where)->count(); // 查询满足要求的总记录数
    	$Page = new Page($count,$pagesize); //分页类
        $prom_list = M('goods')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select(); //活动对应的商品
    	$this->assign('prom_list', $prom_list);
        if(I('is_ajax')){
            return $this->fetch('ajax_discount_list');
        }
    	return $this->fetch();
    }

    /**
     * 商品活动页面
     * $author lxl
     * $time 2017-1
     */
    public function promote_goods(){
        $now_time = time();
        $where = " start_time <= $now_time and end_time >= $now_time ";
        $count = M('prom_goods')->where($where)->count();  // 查询满足要求的总记录数
        $pagesize = C('PAGESIZE');  //每页显示数
        $Page  = new Page($count,$pagesize); //分页类
        $promote = M('prom_goods')->field('id,name,start_time,end_time,prom_img')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();    //查询活动列表
      
        $this->assign('promote',$promote);
        if(I('is_ajax')){
            return $this->fetch('ajax_promote_goods');
        }
        return $this->fetch();
    }

    /**
     * 秒杀列表
     * * $author lxl
     * $time 2017-1
     */
    public function seckill_list(){
        //首页index方法里面有下面代码
        $now_time = time();  //当前时间
        if(is_int($now_time/7200)){      //双整点时间，如：10:00, 12:00
            $start_time = $now_time;
        }else{
            $start_time = floor($now_time/7200)*7200; //取得前一个双整点时间
        }
        $end_time = $start_time+7200;   //结束时间
        $count1 = DB::query("select count(g.goods_id) as count from __PREFIX__goods as g inner join __PREFIX__flash_sale as f on g.goods_id = f.goods_id where start_time = $start_time and end_time = $end_time");     //查询符合要求的总记录数
        $pagesize = C('PAGESIZE');  //每页分页数
        $count = $count1[0]['count'];    //统计数量
        $Page = new Page($count ,$pagesize);    //分页类
        $seckill_list=DB::query("select f.goods_id,f.title,f.price,f.order_num,f.goods_num,g.goods_name,g.market_price from __PREFIX__goods as g inner join __PREFIX__flash_sale as f on g.goods_id = f.goods_id where start_time = $start_time and end_time = $end_time limit ".$Page->firstRow.",".$Page->listRows."");     //获取商品
        $this->assign('seckill_list',$seckill_list);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('now_time',$now_time);
        if(I('is_ajax')){
            return $this->fetch('ajax_seckill_list');
        }
        return $this->fetch();
    }

}